package com.genesis.yunnanji.activity;

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.widget.Toolbar;
import android.text.Html;
import android.view.View;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.genesis.yunnanji.R;
import com.genesis.yunnanji.bean.PavilionDetailsBean;
import com.genesis.yunnanji.config.GenesisApiConfig;
import com.genesis.yunnanji.utils.NetWorkUtils;
import com.google.gson.Gson;
import com.jaeger.library.StatusBarUtil;
import com.lidroid.xutils.BitmapUtils;

import org.json.JSONObject;
import org.xutils.http.RequestParams;
import org.xutils.view.annotation.Event;
import org.xutils.view.annotation.ViewInject;



public class PavilionMain extends BaseActivity {
    @ViewInject(R.id.toolbar_pavilionmain_toolbar)
    private Toolbar mToolbar;
    private String id;
    @ViewInject(R.id.iv_pavilionmain_name)
    private TextView tvName;
    @ViewInject(R.id.iv_pavilionmain_bg)
    private ImageView ivBackdrop;
    @ViewInject(R.id.tv_pavilionmain_intro)
    private TextView tvIntro;
    @ViewInject(R.id.tv_pavilionmain_title)
    private TextView tvTitle;
    @ViewInject(R.id.rl_pavilionmain_intro)
    private RelativeLayout rlIntro;
    @ViewInject(R.id.rl_pavilionmain_scenery)
    private RelativeLayout rlTourist;
    @ViewInject(R.id.rl_pavilionmain_special)
    private RelativeLayout rlSpecial;
    @ViewInject(R.id.rl_pavilionmain_mushroom)
    private RelativeLayout rlMushroom;
    @ViewInject(R.id.rl_pavilionmain_culture)
    private RelativeLayout rlCulture;
    @ViewInject(R.id.rl_pavilionmain_house)
    private RelativeLayout rlHouse;


    private Intent intent;
    private PavilionDetailsBean pavilionDetailsBean;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState, R.layout.activity_pavilion_main);
    }

    @Override
    protected void initView(Bundle savedInstanceState) {
        id = getIntent().getStringExtra("pavilion_id");
        StatusBarUtil.setColor(this, getResources().getColor(R.color.main_color), 2);
        mToolbar.setTitle("");
        setSupportActionBar(mToolbar);
        getSupportActionBar().setDisplayHomeAsUpEnabled(true);
        mToolbar.setNavigationOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
        getData();
    }

    @Override
    protected void setUpView() {

    }

    @Event(value = {R.id.rl_pavilionmain_intro, R.id.rl_pavilionmain_scenery, R.id.rl_pavilionmain_special, R.id.rl_pavilionmain_mushroom, R.id.rl_pavilionmain_culture, R.id.rl_pavilionmain_house}, type = View.OnClickListener.class)
    private void click(View v) {
        switch (v.getId()) {
            case R.id.rl_pavilionmain_intro:
                intent = new Intent(context, PavilionIntro.class);
                intent.putExtra("pavilion_bean", pavilionDetailsBean);
                startActivity(intent);
                break;
            case R.id.rl_pavilionmain_scenery:
                intent = new Intent(context, Tourist.class);
                intent.putExtra("dizhou_id", pavilionDetailsBean.getResult().getInfo().getId());
                startActivity(intent);
                break;
            case R.id.rl_pavilionmain_special:
                intent = new Intent(context, Specialty.class);
                intent.putExtra("title", "云南特产");
                intent.putExtra("id", "1");
                intent.putExtra("dizhou", pavilionDetailsBean.getResult().getInfo().getId());
                startActivity(intent);
                break;
            case R.id.rl_pavilionmain_mushroom:
                intent = new Intent(context, Specialty.class);
                intent.putExtra("title", "云南菌库");
                intent.putExtra("id", "2");
                intent.putExtra("dizhou", pavilionDetailsBean.getResult().getInfo().getId());
                startActivity(intent);
                break;
            case R.id.rl_pavilionmain_culture:
                intent = new Intent(context, Specialty.class);
                intent.putExtra("title", "云南非遗");
                intent.putExtra("id", "15");
                intent.putExtra("dizhou", pavilionDetailsBean.getResult().getInfo().getId());
                startActivity(intent);
                break;
            case R.id.rl_pavilionmain_house:
                intent = new Intent(context, House.class);
                intent.putExtra("dizhou", pavilionDetailsBean.getResult().getInfo().getId());
                startActivity(intent);
                break;
        }
    }

    private void getData() {
        RequestParams params = new RequestParams(GenesisApiConfig.HOST + GenesisApiConfig.PAVILION_DETAILS);
        params.addBodyParameter("id", id);
        NetWorkUtils.doCacheGet(params, new NetWorkUtils.HttpResultListener() {
            @Override
            public void onSuccess(JSONObject jsonObject) {
                Gson gson = new Gson();
                pavilionDetailsBean = gson.fromJson(jsonObject.toString(), PavilionDetailsBean.class);
                tvIntro.setText(Html.fromHtml(pavilionDetailsBean.getResult().getInfo().getDescription()));
                tvName.setText(pavilionDetailsBean.getResult().getInfo().getTitle() + "\n" + pavilionDetailsBean.getResult().getInfo().getCode());
                BitmapUtils bitmapUtils = new BitmapUtils(context);
                bitmapUtils.display(ivBackdrop, GenesisApiConfig.PIC_HOST + pavilionDetailsBean.getResult().getInfo().getCover_img());
                tvTitle.setText(pavilionDetailsBean.getResult().getInfo().getTitle());
            }

            @Override
            public void OnCached(JSONObject jsonObject) {
            }

            @Override
            public void onFinished() {

            }

            @Override
            public void onError(String errorStr) {

            }
        });
    }

}
